package com.kdtech.analyse.video;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <电视台 - 江阴电视台> 解析类
 *
 * @author KK
 *
 */
public class IjiangyinAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://.*.ijiangyin.com/article-[0-9]+-1.html",
				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = null;
		Document doc = Jsoup.parse(htmltxt);

		title =   doc.select("input#newsTitle").val();
		if(StringUtils.isBlank(title)){
			  Elements select = doc.select("div#xm_daohang");
			  if(select!=null&&select.size()>0){
				  title = select.get(0).ownText();
				  title = title.replace(">", "");
			  }
		}
		dateStr = StringUtils.substringBetween(htmltxt,"play_flv(", ");");
		if(StringUtils.isNotBlank(dateStr)){
			try {
				dateStr = URLDecoder.decode(dateStr, "UTF-8");
				date = DateUtils.matchDate(dateStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(2);
			news.setTitle(StringUtils.trimSpace(title));
			news.setDate(date);
			return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (StringUtils.isNotBlank(meta.getUpdateUrl())) {
			String html = CrawlHTML.GetHtml(meta.getUpdateUrl(), "UTF-8");
			if(StringUtils.isNotBlank(html)){
				String clickNumStr = StringUtils.substringBetween(html, "document.write('","');") ;
				Integer clickNum = NumberUtils.getInteger(clickNumStr);
				meta.setClickNum(clickNum);
				return meta;
			}
		}
		return null;
	}

	

	
}
